//
//  MCHomePage_storeinfoView.swift
//  StoreManage
//
//  Created by MC on 2018/6/21.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

typealias MCHomePage_storeinfoViewClosure = (UIViewController) -> ()

class MCHomePage_storeinfoView: UIView {

    var closure : MCHomePage_storeinfoViewClosure?
    
    var dataDict : [String:Any] = [:] {
        didSet {
            let storeName = dataDict["storeName"] as? String ?? ""
            let logoPath = dataDict["logoPath"] as? String ?? ""
            let usersNum = dataDict["usersNum"] as? NSNumber ?? 0
            
            
            let penDeliveryCount = dataDict["penDeliveryCount"] as? NSNumber ?? 0
            let afterSalesCount = dataDict["afterSalesCount"] as? NSNumber ?? 0
            let userCommision = dataDict["userCommision"] as? NSNumber ?? 0
            let clerkNum = dataDict["clerkNum"] as? NSNumber ?? 0
            
            
            iconImageView.sd_setImage(with: URL.init(string: logoPath), placeholderImage: #imageLiteral(resourceName: "Reuse_placeholder_1_1"))
            storeNameLabel.text = storeName
            fansLabel.text = "用户数:" + usersNum.stringValue
            
            waitSendButton.topLabel.text = String(penDeliveryCount.intValue)
            afterSaleButton.topLabel.text = String(afterSalesCount.intValue)
            commissionButton.topLabel.text = String(userCommision.intValue)
            managerButton.topLabel.text = String(clerkNum.intValue)
        }
    }

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.frame = CGRect.init(x: 0, y: 0, width: MCScreenWidth, height: 195)

        
        self.backgroundColor = UIColor.white
        self.addSubview(topView)
        topView.snp.remakeConstraints { (make) ->Void in
            make.left.right.top.equalTo(self)
            make.height.equalTo(115)
        }
        
        self.addSubview(contentView)
        contentView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(self).inset(UIEdgeInsetsMake(15, 15, 15, 15))
        }
        
        contentView.addSubview(iconImageView)
        iconImageView.snp.remakeConstraints { (make) ->Void in
            make.left.top.equalTo(20)
            make.size.equalTo(CGSize.init(width: 50, height: 50))
        }
        
        contentView.addSubview(storeNameLabel)
        storeNameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(iconImageView.snp.right).offset(10)
            make.bottom.equalTo(iconImageView.snp.centerY).offset(1)
        }
        
        contentView.addSubview(fansLabel)
        fansLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(iconImageView.snp.right).offset(10)
            make.top.equalTo(iconImageView.snp.centerY).offset(4)
        }
        
        let width = (MCScreenWidth - 30) / 4
        let height : CGFloat = 50
        
        contentView.addSubview(waitSendButton)
        waitSendButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(0)
            make.top.equalTo(iconImageView.snp.bottom).offset(20)
            make.size.equalTo(CGSize.init(width: width, height: height))
        }
        
        contentView.addSubview(afterSaleButton)
        afterSaleButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(waitSendButton.snp.right)
            make.top.equalTo(iconImageView.snp.bottom).offset(20)
            make.size.equalTo(CGSize.init(width: width, height: height))
        }

        
        contentView.addSubview(commissionButton)
        commissionButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(afterSaleButton.snp.right)
            make.top.equalTo(iconImageView.snp.bottom).offset(20)
            make.size.equalTo(CGSize.init(width: width, height: height))
        }

        
        contentView.addSubview(managerButton)
        managerButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(commissionButton.snp.right)
            make.top.equalTo(iconImageView.snp.bottom).offset(20)
            make.size.equalTo(CGSize.init(width: width, height: height))
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    @objc func funcButtonClicked(sender:UIButton) {
        let tag = sender.tag - 1000
        
        switch tag {
        case 0:
            let vc = MCOrderPageViewController()
            vc.currentPage = 1
            self.closure?(vc)
        case 1:
            
            // 只有店主才可查看
            if MCUserinfo_getLevel() >= MCRole_manager {
                let vc = MCAfterSalePageViewController()
                self.closure?(vc)
            } else {
                MCHUD_text(message: "只有店长才可查看", view: self, delay: 1.2)
            }
            
        case 2:
            let vc = MCMyCommissionViewController()
            vc.clerkId = MCUserinfo_getUserId()
            self.closure?(vc)
        case 3:
            if MCUserinfo_getLevel() >= MCRole_manager {
                let vc = MCClerkManageViewController()
                closure?(vc)
            } else {
                MCHUD_text(message: "只有店长才可查看", view: self, delay: 1.2)
            }
        default:
            break
        }
        
    }

    
    lazy var topView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCMain
        return view
    }()
    
    lazy var contentView: UIView = {
        let view = UIView()
        view.layer.cornerRadius = 5
        view.backgroundColor = UIColor.white
        
        view.layer.shadowColor = UIColor.MCGray_middle.cgColor
        view.layer.shadowOpacity = 0.8
        view.layer.shadowOffset = CGSize(width: 0, height: 0)
        view.layer.shadowRadius = 4
        view.layer.masksToBounds = false

        return view
    }()
    
    
    lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Reuse_placeholder_1_1")
        imageView.layer.cornerRadius = 25
        imageView.layer.masksToBounds = true
        imageView.layer.borderColor = UIColor.MCGray_light.cgColor
        imageView.layer.borderWidth = 1
        return imageView
    }()
    
    lazy var storeNameLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(16)
        label.textColor = UIColor.MCWords
        label.text = "XXX"
        return label
    }()
    
    lazy var fansLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray_middle
        label.text = "xxx"
        return label
    }()
    
    lazy var waitSendButton: MCStoreButton = {
        let button = MCStoreButton()
        button.topLabel.text = "0"
        button.bottomLabel.text = "待发货"
        button.tag = 1000
        button.addTarget(self, action: #selector(funcButtonClicked(sender:)), for: .touchUpInside)
        return button
    }()
    
    lazy var afterSaleButton: MCStoreButton = {
        let button = MCStoreButton()
        button.topLabel.text = "0"
        button.bottomLabel.text = "售后"
        button.tag = 1001
        button.addTarget(self, action: #selector(funcButtonClicked(sender:)), for: .touchUpInside)
        return button
    }()

    lazy var commissionButton: MCStoreButton = {
        let button = MCStoreButton()
        button.topLabel.text = "0"
        button.bottomLabel.text = "佣金"
        button.tag = 1002
        button.addTarget(self, action: #selector(funcButtonClicked(sender:)), for: .touchUpInside)
        return button
    }()

    lazy var managerButton: MCStoreButton = {
        let button = MCStoreButton()
        button.topLabel.text = "0"
        button.bottomLabel.text = "管理人员"
        button.lineView.isHidden = true
        button.tag = 1003
        button.addTarget(self, action: #selector(funcButtonClicked(sender:)), for: .touchUpInside)
        return button
    }()
}


class MCStoreButton: UIButton {
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(topLabel)
        topLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(self)
            make.bottom.equalTo(self.snp.centerY).offset(-4)
            make.height.equalTo(20)
        }
        
        self.addSubview(bottomLabel)
        bottomLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(self)
            make.top.equalTo(self.snp.centerY).offset(4)
            make.height.equalTo(20)
        }
        self.addSubview(lineView)
        lineView.snp.remakeConstraints { (make) ->Void in
            make.top.equalTo(topLabel).offset(5)
            make.bottom.equalTo(bottomLabel).offset(-5)
            make.right.equalTo(self).offset(-1)
            make.width.equalTo(1)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var topLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(18)
        label.textColor = UIColor.MCWords
        label.text = "0"
        label.textAlignment = NSTextAlignment.center
        return label
    }()

    lazy var bottomLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(15)
        label.textColor = UIColor.MCGray_middle
        label.text = "XXX"
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
}
